package com.example.brain2;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
//from the other src
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import android.os.Handler;
import android.view.View.OnClickListener;

public class ImageAdapter extends BaseAdapter {

	// private Context mContext;
	// public Integer [] allPictures;
	// public Integer [] images = {R.drawable.idiot, R.drawable.images,
	// R.drawable.kartcsrg,
	// R.drawable.nouturn, R.drawable.ic_launcher,
	// R.drawable.geometry_mountain_fs};
	//
	// public Integer[] getImages() {
	// return images;
	// }
	//
	// public void setImages(Integer[] images) {
	// this.images = images;
	// }
	//
	// // Constructor
	// public ImageAdapter(Context context){
	// this.mContext = context;
	// // this.allPictures = addPictures();
	// }
	//
	// // public Integer [] addPictures () {
	// // allPictures = new Integer[8];
	// // Resources res = mContext.getResources();
	// //
	// // for (int i = 0; i < allPictures.length; i++) {
	// // allPictures[i] = R.drawable.idiot;
	// // }
	// // return allPictures;
	// // }
	//
	// @Override
	// public int getCount() {
	// return images.length;
	// }
	//
	// @Override
	// public Object getItem(int position) {
	// return images[position];
	// }
	//
	// @Override
	// public long getItemId(int position) {
	// return 0;
	// }
	//
	// @Override
	// public View getView(int position, View convertView, ViewGroup parent) {
	// ImageView imageView = new ImageView(mContext);
	// imageView.setImageResource(images[position]);
	// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
	// imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
	// return imageView;
	// }

	// code from other src////////////////

	private static final int GAME_SIZE = ActualGameActivity.getGAME_SIZE();
	private List<Cell> matrix;
	private Context context;
	private Set<Integer> randomlyPopulatedCells;
	private OnImageClickedListener listener;
	private List<Integer> lockedPositions;

	// private List<Integer> randomCards;

	// inner class containing the 2 sides of each image
	static class ViewHolder {
		private ImageView frontImage;
		private ImageView backImage;
	}

	// CONSTRUCTOR!!!
	public ImageAdapter(Context context, List<Cell> matrix,
			OnImageClickedListener listener) {
		super();
		this.matrix = matrix;
		this.context = context;
		randomlyPopulatedCells = new HashSet<Integer>();
		lockedPositions = new ArrayList<Integer>();
		this.listener = listener;
//		generateRandomCells();
	}

	@Override
	public int getCount() {
		return matrix.size();
	}

	@Override
	public Object getItem(int position) {
		return matrix.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO implement if needed
		return 0;
	}

	// WHAT ON MARS IS THAT ACTUALLY DOING?!?!?!
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder = null;

		if (view == null) {
			view = View.inflate(context, R.layout.cell_layout, null);
			holder = new ViewHolder();
			holder.backImage = (ImageView) view.findViewById(R.id.back_image);
			holder.frontImage = (ImageView) view.findViewById(R.id.front_image);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		// populates the matrix with images, if empty
		if (randomlyPopulatedCells.contains(Integer.valueOf(position))
				&& matrix.get(position).isEmpty()) {
			final Image image = createImage(
					(ImageView) view.findViewById(R.id.back_image),
					(ImageView) view.findViewById(R.id.front_image));
			matrix.get(position).setCard(image);
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (listener != null) {
						int clickedPosition = (Integer) v
								.getTag(R.string.card_position);
						if (!lockedPositions.contains(clickedPosition)) {
							listener.onImageClicked(
									(Image) v.getTag(R.string.card_type),
									clickedPosition);
							image.rotate(0, 90);
						}
					}
				}
			});

			view.setTag(R.string.card_position, position);
		}
		return view;
	}

	// generates random numbers in a set from 0 to size/2 of matrix
	private void generateRandomCells() {
		Random random = new Random();
		for (int i = 0; i < GAME_SIZE / 2 + 1; i++) {
			int randomPlace = random.nextInt(GAME_SIZE / 2);
			if (randomlyPopulatedCells.contains(Integer.valueOf(randomPlace))) {
				i--;
			} else {
				randomlyPopulatedCells.add(Integer.valueOf(randomPlace));
			}
		}
	}

	// create an Image with both sides initially questions
	private Image createImage(ImageView frontImage, ImageView backImage) {
		// Collections.shuffle(randomCards);
		Bitmap backPicture = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.question_mark);
		Bitmap frontPicture = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.question_mark);

		frontImage.setImageBitmap(frontPicture);
		backImage.setImageBitmap(backPicture);
		Image result = new Image(frontImage, backImage);
		return result;
	}

	// adds this image's position to the list of locked (guessed images)
	public void lockCard(int position) {
		lockedPositions.add(Integer.valueOf(position));
	}

	public void closeCards(final int position) {
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				matrix.get(position).getCard().flipImage();

			}
		}, 600);
	}

	/////EXPERIMENTS
	// adds pictures in a Integer array and returns it
	public Image[] addPictures() {
		Image[] picArray = new Image[GAME_SIZE];// maybe this is better?

		ImageView image = new ImageView(context);
		Resources res = context.getResources();
		for (int i = 0; i < GAME_SIZE; i++) {
			image.setImageDrawable(res.getDrawable(i));
		}

		// barely
		ImageView imgView = new ImageView(context);
		Random rand = new Random();
		int rndInt = rand.nextInt(GAME_SIZE) + 1; // n = the number of images,
													// that start at idx 1
		String imgName = "img" + rndInt;
		int id = res.getIdentifier(imgName, "drawable", "folder");
		imgView.setImageResource(id);

		return picArray;
	}

	public void aaaaa() {
		Field[] idFields = R.drawable.class.getFields();
		int [] resArray = new int[idFields.length];
		for (int i = 0; i < idFields.length; i++) {
			try {

				resArray[i] = idFields[i].getInt(null);

			} catch (IllegalArgumentException e) {
				
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				
				e.printStackTrace();
			}
		}
	}
	
	
		

}
